package Application;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import Application.Components.Schedule.ScheduleDataManager;


/**
 * Class representing ServerHandler
 * 	just collecting Data, to be used for rendering
 * 	in Application class
 * @author drahomir.karchnak
 */

public class ServerHandler implements Runnable{
	public static final String IMAGES_DIR	= "\\\\gandalf.sospost.local\\workspace\\drahomir.karchnak\\ScreenSaverSSINFIS\\res\\";	
	public static final int UPDATE_PERIOD	= 10000;
	
	private Thread thread;
	private boolean running;
	
	private int roomNumber;
	private ScheduleDataManager sdm;
	
	private List<String> imageFiles = new LinkedList<>();
	
	/**
	 * Constructor
	 * Creates new thread for ServerHandler
	 */
	
	public ServerHandler(){
		this.thread = new Thread(this, "ServerHandling Thread");
		this.thread.start();
	}
	/**
	 * Start the ServerHandler
	 */
	
	@Override
	public void run() {
		System.out.println("[i] " + thread.getName() + " is running!"); 
		this.running = true;
	
		updateImageFiles();
		
		this.roomNumber = getRoomNumber();
		System.out.println("[i] Room number set to " + roomNumber);

		this.roomNumber = 11;
		while(running){
			sdm = new ScheduleDataManager(roomNumber);
			sdm.connect();
			System.out.println("[i] Updating ServerHandler!");
			
			updateImageFiles();
			sdm.updateScheduleData();
			
			try {
				Thread.sleep(UPDATE_PERIOD);
			} catch (InterruptedException e) {
				running = false;
			}
			sdm.cleanUp();
		}
		sdm.cleanUp();
		System.exit(0);
	}
	
	/**
	 * Get all image files in IMAGES_DIR directory!
	 */
	
	public void updateImageFiles(){
		File[] filesTemp = new File(IMAGES_DIR).listFiles();
		
		imageFiles.clear();
		
		for(int i = 0; i < filesTemp.length; i++){
			
			String fileName		= filesTemp[i].getName();
			String extension	= fileName.substring(fileName.length()-3, fileName.length());
			
			if(extension.equals("jpg")) imageFiles.add(fileName);
		}
		
		System.out.print("[i] Files: (");
		for(int j = 0; j < imageFiles.size(); j++){
			System.out.print(imageFiles.get(j) + " ");
		}
		System.out.println(") have been loaded!");
	}
	
	/**
	 * Return array of String (File Names)
	 * @return
	 */
	
	public String[] getImageFiles(){
		return imageFiles.toArray(new String[imageFiles.size()]);
	}
	
	/**
	 * Return status of ServerHandling
	 * @return true(running), false(not running)
	 */
	
	public boolean isRunning(){
		return running;
	}
	
	/**
	 * Immediately stop ServerHandler
	 */
	
	public void stop(){
		//if Thread is sleeping, "awakes it" (throws InterruptedException)
		//in while(running) loop
		if(thread.getState() == Thread.State.TIMED_WAITING) thread.interrupt();
		running = false;
	}
	
	/**
	 * Return room number
	 * @return
	 */
	
	public int getRoomNumber(){
		try {
			String address = InetAddress.getLocalHost().getHostAddress();
			String[] octets = address.split("[.]");
			
			int num = Integer.parseInt(octets[2]);
			
			if(num > 100){
				num -= 100;
				return num;
			}
			else return new Random().nextInt(4) + 13;
					
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
	
	public ScheduleDataManager getScheduleDataManager(){
		return this.sdm;
	}
}
